# -*- coding: utf-8 -*-
# Multi-purpose AssembleR Utility (MARU)

import os
import sys
import argparse

# Add hare folder to system path.
maru_path, _ = os.path.split(__file__)
sys.path.insert(0, maru_path)

from mtl.mtl import generate_mtl
from hla.hla import hla_assemble


def parse_args():
    # Create argument parser.
    parser = argparse.ArgumentParser(description='Multi-purpose Assembler Utility (MARU)')
    # Add argument rules.
    parser.add_argument('-gmtl', '--generate-mtl', dest='generate-mtl', action='store_true',
                        help='Generate the MARU template library.')

    parser.add_argument('file', nargs='?', default='',
                        help='Python assembly files.')
    parser.add_argument('--arch', dest='arch',
                        help='The arch path of the project.')
    parser.add_argument('-o', '--output', dest='out',
                        help='Output file path.')
    parser.add_argument('-f', '--format', dest='format',
                        help='Output file format.')
    parser.add_argument('--mtl', dest='mtl',
                        help='MARU standard library path used by the assembly file.')
    # Parse the argument.
    return parser


def main():
    # Parse the arguments.
    args_parser = parse_args()
    args = args_parser.parse_args().__dict__
    # Check the working format.
    if args['generate-mtl']:
        if args['arch'] is None:
            raise Exception('Please specify the arch directory path by using --arch [PATH].')
        # Generate the MTL with the specific arch directory.
        generate_mtl(args['arch'])
    elif len(args['file']) > 0:
        # We need the RTL library path.
        if args['mtl'] is None:
            raise Exception('Please provided the MARU template library path used in the assembly file.')
        # Assemble the file.
        hla_assemble(args['file'], args['mtl'], args['format'], args['out'], args['arch'])
    else:
        # Show help of maru.
        args_parser.print_help()


if __name__ == '__main__':
    main()